﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.Win32.Dialogs;
using ArtOfTest.WebAii.Controls.HtmlControls;

namespace SM2DashBoard
{
    public class DashboardMainPage
    {
        public static void clickAddPanels()
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                string str = TB.Text.ToString();
                if (str.Equals("Add Panels"))
                {
                    TB.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    break;
                }
            }
        }

        public static void clickEditDashboard()
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                string str = TB.Text.ToString();
                if (str.Equals("Edit Dashboard"))
                {
                    TB.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    break;
                }
            }
        }

        public static void clickRemoveDashboard()
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                string str = TB.Text.ToString();
                if (str.Equals("Remove Dashboard"))
                {
                    //ConfirmDialog cd = new ConfirmDialog(Utilities.GlobalObject.MANAGER.ActiveBrowser, DialogButton.OK);
                    //Utilities.GlobalObject.MANAGER.DialogMonitor.AddDialog(cd);
                    //Utilities.GlobalObject.MANAGER.DialogMonitor.Start();

                    //Utilities.GlobalObject.MANAGER.DialogMonitor.AddDialog(cd);
                    //Utilities.GlobalObject.MANAGER.DialogMonitor.Start();

                    //var confirmDialog = new ConfirmDialog(Utilities.GlobalObject.MANAGER.ActiveBrowser, DialogButton.OK);
                    //Utilities.GlobalObject.MANAGER.DialogMonitor.AddDialog(confirmDialog);
                    //Utilities.GlobalObject.MANAGER.DialogMonitor.Start();

                    //TB.User.Click();
                    //confirmDialog.WaitUntilHandled(5000);

                    //Utilities.GlobalObject.MANAGER.DialogMonitor.Stop();
                }
            }
        }

        public static void clickAddDashboard()
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                try
                {
                    string str = TB.Text.ToString();
                    if (str.Equals("Add Dashboard"))
                    {
                        TB.User.Click();
                        System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                        break;
                    }
                }
                catch { }
            }
        }

        public static void clickAbout()
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                string str = TB.Text.ToString();
                if (str.Equals("About"))
                {
                    TB.User.Click();
                    System.Threading.Thread.Sleep(1000);
                    break;
                }
            }
        }

        public static void selectDashboard(string strDBName)
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<TextBlock> iListTB = tabControl.Find.AllByType<TextBlock>();
            int intiListTB = iListTB.Count;
            for (int i = 0; i < intiListTB; i++)
            {
                TextBlock TB = iListTB[i];
                string str = TB.Text.ToString();
                if (str.Equals(strDBName))
                {
                    TB.User.Click();
                    waitForAllPanelsLoaded();
                    break;
                }
            }
        }

        public static void waitForAllPanelsLoaded()
        {
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
            IList<Button> iList = tabControl.Find.AllByName<Button>("SettingsButton");
            int intCountIList = iList.Count + 1;
            Utilities.GlobalObject.LOGGER.log("Call waitForAllPanelsLoaded() with intCountIList = " + intCountIList);
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND * intCountIList);
        }

        public static void selectPanel(int intPositionPanel, string strSelectedPanel)
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");

            //Open expected panel
            IList<Button> iList = tabControl.Find.AllByName<Button>("SettingsButton");
            Button btnEditSettings = iList[intPositionPanel];
            btnEditSettings.User.Click();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
        }

        public static void clickRestoreMaximize(int intPositionPanel)
        {
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");

            //Open expected panel
            IList<Button> iList = tabControl.Find.AllByName<Button>("MinMaxButton");
            Button btnEditSettings = iList[intPositionPanel];
            btnEditSettings.User.Click();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
            btnEditSettings.User.Click();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
        }

        public static int findPositionPanel(string strPanel)
        {
            //Open all panels
            TabControl tabControl = Utilities.GlobalObject.SILVERLIGHTAPP.FindName<TabControl>("tabDashboards");
            IList<Button> iList = tabControl.Find.AllByName<Button>("SettingsButton");
            int intCountIList = iList.Count;
            Utilities.GlobalObject.LOGGER.log("Total SettingsButton = " + intCountIList.ToString());
            for (int intIndex = 0; intIndex < intCountIList; intIndex++)
            {
                Button btnItem = iList[intIndex];
                try
                {
                    btnItem.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                    btnItem.User.Click();
                    System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_INTERVAL_MILISECOND);
                }
                catch { }
            }
            //Loop over all text boxes to find expected name of panel
            IList<TextBox> iListTextbox = Utilities.GlobalObject.SILVERLIGHTAPP.Find.AllByName<TextBox>("txtName");
            string strDB = "";
            int intCountIListTextbox = iListTextbox.Count;
            int i = 0;
            for (; i < intCountIListTextbox; i++)
            {
                TextBox tb = iListTextbox[i];
                strDB = tb.Text;
                Utilities.GlobalObject.LOGGER.log(strDB);
                if (strDB.Equals(strPanel)) return i;
            }
            return 0;
        }
    }
}
